package net.tv90.bfmccore.battlepass;

import net.tv90.bfmccore.constant.RewardConstant;
import net.tv90.bfmccore.event.*;
import org.bitterorange.flansmodslain.models.PlayerSlainEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

public class BattlepassListener implements Listener {

    @EventHandler
    public void onPlayerBattlepassUpgraded(PlayerBattlepassUpgradedEvent e) {
        Player player = e.getPlayer();
        System.out.println(player.getName() + "的通行证升级到了" + e.getLevel() + "级，当前点数为" + e.getPoint() + "点");

        for (int i = 1; i <= e.getLevel(); i++) {
            if (!player.hasPermission("bfmc.bps." + Battlepass.CURRENT_BATTLE_PASS_NAME + "." + i)) {
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() +
                        " permission set bfmc.bps." + Battlepass.CURRENT_BATTLE_PASS_NAME + "." + i);
            }
        }
        player.sendMessage("§e[战斗通行证]恭喜你的战斗通行证升级到了" + e.getLevel() + "级，当前通行证点数为" + e.getPoint() + "点");

        if (!player.hasPermission("bfmc.bps.buy." + Battlepass.CURRENT_BATTLE_PASS_NAME)) {
            player.sendMessage("§e[战斗通行证]你还未购买" + Battlepass.CURRENT_BATTLE_PASS_NAME.toUpperCase() +
                    "赛季战斗通行证，输入 §b/bps buy §e花费 §b" + Battlepass.MONEY_TO_BUY_BATTLEPASS + "§e 游戏币购买通行证即可获得丰厚奖励");
        }
        Battlepass.storeBattlepassData();
    }

    @EventHandler
    public void onPlayerSlain(PlayerSlainEvent e) {
        //玩家击杀玩家
        if (!e.isKilledAI() && !e.isKillerAI()) {
            Battlepass.addBattlepassPoint(e.getPlayerKiller(), (int) (100 * RewardConstant.POINT_MAGNIFICATION));
            Battlepass.addBattlepassPoint(e.getPlayerKilled(), (int) (30 * RewardConstant.POINT_MAGNIFICATION));
        }
        //玩家击杀AI
        else if (!e.isKillerAI() && e.isKilledAI()) {
            Battlepass.addBattlepassPoint(e.getPlayerKiller(), (int) (100 * RewardConstant.POINT_MAGNIFICATION));
        }
        //AI击杀玩家
        else if (e.isKillerAI() && !e.isKilledAI()) {
            Battlepass.addBattlepassPoint(e.getPlayerKilled(), (int) (20 * RewardConstant.POINT_MAGNIFICATION));
        }
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
        if (!e.getPlayer().hasPermission("bfmc.bps." + Battlepass.CURRENT_BATTLE_PASS_NAME + ".1")) {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + e.getPlayer().getName() +
                    " permission set bfmc.bps." + Battlepass.CURRENT_BATTLE_PASS_NAME + ".1");
        }
    }

    @EventHandler
    public void onFlagCaptured(FlagCapturedEvent e) {
        Player player = e.getPlayer();
        Battlepass.addBattlepassPoint(player, (int) (400 * RewardConstant.POINT_MAGNIFICATION));
    }

    @EventHandler
    public void onFlagNeutralized(FlagNeutralizedEvent e) {
        Player player = e.getPlayer();
        Battlepass.addBattlepassPoint(player, (int) (100 * RewardConstant.POINT_MAGNIFICATION));
    }

    @EventHandler
    public void onRibbonGet(RibbonGetEvent e) {
        Player player = e.getPlayer();
        Battlepass.addBattlepassPoint(player, (int) (200 * RewardConstant.POINT_MAGNIFICATION));
    }

    @EventHandler
    public void onServiceStarGet(ServiceStarGetEvent e) {
        Player player = e.getPlayer();
        Battlepass.addBattlepassPoint(player, (int) (2000 * RewardConstant.POINT_MAGNIFICATION));
    }

    @EventHandler
    public void onGameWin(GameWinEvent e) {
        for (Player player : e.getPlayerSet()) {
            Battlepass.addBattlepassPoint(player, (int) (2000 * RewardConstant.POINT_MAGNIFICATION));
        }
    }

    @EventHandler
    public void onGameLose(GameLoseEvent e) {
        for (Player player : e.getPlayerSet()) {
            Battlepass.addBattlepassPoint(player, (int) (1000 * RewardConstant.POINT_MAGNIFICATION));
        }
    }

    @EventHandler
    public void onGameSame(GameSameEvent e) {
        for (Player player : e.getPlayerSet()) {
            Battlepass.addBattlepassPoint(player, (int) (1500 * RewardConstant.POINT_MAGNIFICATION));
        }
    }


}
